<template>
  <div class="station-top">
    <el-popover placement="bottom" width="400" trigger="hover" v-model:visible="showPopover"
      popper-class="station-popover">
      <template #reference>
        <div v-if="siteGroup?.length > 0" class="site-groups">
          <p :title="currentSite.name">
            切换站点
          </p>
          <el-icon v-if="!showPopover">
            <CaretBottom />
          </el-icon>
          <el-icon v-else>
            <CaretTop />
          </el-icon>
        </div>
      </template>
      <div class="site-list">
        <div v-for="item in siteGroup" :key="item.id" class="site-item"
          :class="{ active: String(item.id) === String(currentSite.id) }" @click="handleChangeSite(item)">
          <div class="highlight-block" :class="{ active: String(item.id) === String(currentSite.id) }"></div>
          <div class="site-info">
            <div class="site-name">
              <div class="site-name-item">{{ item.name }}</div>
              <el-tag :type="item.state === 0 ? 'success' : 'danger'" size="small" effect="light" class="state-tag">
                {{ item.state === 0 ? '运行中' : '已关停' }}
              </el-tag>
            </div>
            <a class="site-url" :href="item.domain + item.uri" target="_blank">{{ item.domain + item.uri }}</a>
          </div>
        </div>
      </div>
    </el-popover>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { CaretBottom, CaretTop } from '@element-plus/icons-vue'

const router = useRouter()
const store = useStore()
const showPopover = ref(false)
const currentSite = computed(() => store.state.site.currentSite || {})
const siteGroup = computed(() => store.state.site.siteGroup)

const handleChangeSite = site => {
  if (String(currentSite.value.id) !== String(site.id)) {
    store.commit('site/SET_CURRENT_SITE', site)
    store.commit('category/SET_SITE_CATEGORY_TREE', { siteId: site.id })
    showPopover.value = false
    if (router.currentRoute.value.path.includes('/stationContent')) {
      router.push({ path: '/content2/contentManage/stationContent' })
      setTimeout(() => {
        router.go(0)
      }, 0)
    } else {
      router.go(0)
    }
  }
}
</script>

<style scoped lang="scss">
.station-top {
  display: flex;
  align-items: center;
  height: 100%;

  .site-groups {
    height: 30px;
    cursor: pointer;
    padding: 0 15px;
    display: flex;
    align-items: center;

    &:hover {
      color: $mainColor;
    }

    p {
      @include textEllipsis(30px, 1);
      font-size: 12px;
    }

    i {
      font-size: 14px;
      margin: 0 5px;
    }
  }
}

.site-list {
  max-height: 60vh;
  overflow-y: auto;

  .site-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eee;
    overflow: hidden;
    cursor: pointer;

    &:last-child {
      border-bottom: none;
    }

    &.active {
      background: #f5f7fa;
    }

    .highlight-block {
      width: 6px;
      height: 72px;
      margin-right: 12px;
      background: #fff;
      transition: background 0.3s;

      &.active {
        background: $mainColor;
      }
    }

    .site-info {
      flex: 1;

      &:hover {
        .site-url {
          color: var(--el-color-primary);
        }
      }

      .site-name {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 14px;
        font-weight: 500;
        color: #303133;
        margin-bottom: 8px;
        transition: color 0.3s;

        .site-name-item {
          max-width: 250px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }

        .state-tag {
          font-weight: normal;
        }
      }

      .site-url {
        display: inline-block;
        font-size: 13px;
        color: #909399;
        cursor: pointer;
        max-width: 300px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
    }
  }
}

:deep(.el-dialog__header) {
  margin-right: 0;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

:deep(.el-dialog__body) {
  padding: 0;
}

:deep(.el-dialog__headerbtn) {
  top: 20px;
}
</style>

<style lang="scss">
.station-popover {
  width: 350px !important;
  padding: 0 !important;
  border-radius: 8px;
}
</style>